﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using CE.Image;
using CE.User;
using CE.Mail;

public partial class user_ChangePassword : System.Web.UI.Page
{

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteUser))
            {
                Response.Redirect("~/SiteUserSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {

        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("customer");
        if (Page.IsValid)
        {
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.CheckEmail(Session[CE.Global.General.UserEmail].ToString(), Convert.ToInt32(CE.Global.CEUserType.SiteUser));
            if (_account != null)
            {
                string _oldPassword = txtOldPassword.Text;
                string _newPassword = txtNewPassword.Text;
                if (_account.AuthCode == _oldPassword)
                {
                    _account.AuthCode = _newPassword;
                    if (_accountdb.Insert(_account) > 0)
                    {
                        AccountStatusDB _accountStatusDB = new AccountStatusDB();
                        if (_accountStatusDB.UpdatePasswordChangedDate(_account.CEID, CE.Global.General.GetUTCDateTime()))
                        {
                            lblMessage.Text = Resources.Menu.CPmsgPasswordChanged;// "New password has been changed. Redireting to home page.";
                            btnSubmit.Enabled = false;
                            Response.Redirect("~/SiteUserHome.aspx");
                        }
                        else
                        {
                            lblMessage.Text = Resources.Menu.CPmsgErrorPasswordChange;// "Error in new password change process.";
                        }
                    }
                    else
                    {
                        lblMessage.Text = Resources.Menu.CPmsgFailedPasswordChange;// "Change password failed!Try again later!";
                    }
                }
                else
                {
                    lblMessage.Text = Resources.Menu.CPmsgOldPasswordWrong;// "Old password is wrong.";
                }
            }
            else
            {
                lblMessage.Text = Resources.Menu.CPmsgNoEmailFound;// "No such email found.!";
            }
        }
    }

    protected void CheckPasswordLength(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the lenght of value is < 7 or Is Number Value.
            string valArgs = args.Value;
            if (valArgs.IndexOf(' ') != -1 || valArgs.IndexOf('\'') != -1 ||
                valArgs.IndexOf('\"') != -1 || valArgs.IndexOf('~') != -1)
            {
                ((CustomValidator)source).ErrorMessage = Resources.Menu.CPmsgPasswordInvalidChar;// "Password include invalid character or space!";
                ((CustomValidator)source).Text = Resources.Menu.CPmsgPasswordInvalidChar;// "Password include invalid character or space!";
                args.IsValid = false;
            }
            else if (args.Value.Trim().Length < 8)
            {
                ((CustomValidator)source).ErrorMessage = Resources.Menu.CPmsgPasswordLengthMinimum8;// "Password length should be at 8!";
                ((CustomValidator)source).Text = Resources.Menu.CPmsgPasswordLengthMinimum8;// "Password length should be at 8!";
                args.IsValid = false;
            }
            else if (args.Value.Trim().Length > 20)
            {
                ((CustomValidator)source).ErrorMessage = Resources.Menu.CPmsgPasswordLengthMaximum20;// "Password length should not be over 20!";
                ((CustomValidator)source).Text = Resources.Menu.CPmsgPasswordLengthMaximum20;// "Password length should not be over 20!";
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    public string CreateSalt(int size)
    {
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        Byte[] buff = new Byte[size];
        rng.GetBytes(buff);

        return Convert.ToBase64String(buff);
    }

}
